<template>
  <div id="app">
    <nav>
      <template v-if="!isLoggedIn">
        <!-- 登录链接被移除 -->
      </template>
      <template v-else>
        <router-link to="/guestbook">留言本</router-link> |
        <a @click="logout">退出登录</a>
      </template>
    </nav>
    <router-view></router-view>
  </div>
</template>

<script>
import axios from 'axios'

axios.defaults.baseURL = 'http://localhost:8080'  // 替换为您的后端 API 地址

export default {
  name: 'App',
  data() {
    return {
      isLoggedIn: false
    }
  },
  created() {
    // 检查本地存储中是否有token
    const token = localStorage.getItem('token')
    if (token) {
      this.isLoggedIn = true
      axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
    }
  },
  methods: {
    logout() {
      this.isLoggedIn = false
      localStorage.removeItem('token')
      delete axios.defaults.headers.common['Authorization']
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
nav {
  padding: 20px;
  text-align: right;
}

nav a {
  margin-left: 10px;
}
</style>
